package com.qen.fx.custom.impl.pwd;

import com.qen.App;
import com.qen.fx.ViewIndex;
import com.qen.fx.custom.CustomBusiness;
import com.qen.plugin.notification.NotificationTool;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import lombok.Data;

import java.io.IOException;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Data
public class TokenCreate extends CustomBusiness {

    @FXML
    private CheckBox checkBigChar;
    @FXML
    private CheckBox checkNumber;
    @FXML
    private CheckBox checkSmallChar;
    @FXML
    private CheckBox checkSymbol;
    @FXML
    private Label labelLens;
    @FXML
    private Slider sliderLens;
    @FXML
    private TextArea textToken;
    @FXML
    private Button buttonCopy;
    @FXML
    private Button buttonRefresh;
    @FXML
    private VBox createTokenVbox;
    @FXML
    private HBox inputHBox;
    @FXML
    private HBox outputHBox;
    @FXML
    private HBox checkHBox2;
    @FXML
    private HBox checkHBox1;

    @FXML
    private VBox vBoxBigChar;
    @FXML
    private VBox vBoxNumber;
    @FXML
    private VBox vBoxSmallChar;
    @FXML
    private VBox vBoxSymbol;

    @FXML
    public void clickCopy() {
        String token = textToken.getText();
        ClipboardContent content = new ClipboardContent();
        content.putString(token);
        Clipboard clipboard = Clipboard.getSystemClipboard();
        clipboard.setContent(content);
        NotificationTool.message("复制成功！");
    }

    @FXML
    public void clickRefresh() {
        boolean bigChar = checkBigChar.isSelected();
        boolean number = checkNumber.isSelected();
        boolean smallChar = checkSmallChar.isSelected();
        boolean symbol = checkSymbol.isSelected();
        double lens = sliderLens.getValue();
        labelLens.setText("长度(" + (int) lens + ")");
        String token = randomString(bigChar, number, smallChar, symbol, (int) lens);
        textToken.setText(token);
    }

    public TokenCreate() {

        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(ViewIndex.TokenCreateFXML));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        TokenCreate controller = fxmlLoader.getController();
        controller.setId("tokenCreate" + System.currentTimeMillis());
        inputHBox.setPrefWidth(ViewIndex.contentPreferWidth);
        outputHBox.setPrefWidth(ViewIndex.contentPreferWidth);
        checkHBox1.setPrefWidth(ViewIndex.contentPreferWidth);
        checkHBox2.setPrefWidth(ViewIndex.contentPreferWidth);
        inputHBox.setPrefWidth(ViewIndex.contentPreferWidth);
        createTokenVbox.setPrefWidth(ViewIndex.contentPreferWidth);
        createTokenVbox.setAlignment(Pos.TOP_CENTER);
        vBoxBigChar.setPrefWidth(ViewIndex.contentPreferWidth / 2);
        vBoxSmallChar.setPrefWidth(ViewIndex.contentPreferWidth / 2);
        vBoxSymbol.setPrefWidth(ViewIndex.contentPreferWidth / 2);
        vBoxNumber.setPrefWidth(ViewIndex.contentPreferWidth / 2);
        sliderLens.setPrefWidth(ViewIndex.contentPreferWidth - 300);
        textToken.setPrefWidth(ViewIndex.contentPreferWidth - 300);

//        controller.init();
    }

    @Override
    public VBox init() {
        VBox vb = new VBox();
        vb.setPrefWidth(ViewIndex.contentPreferWidth);
        vb.setPrefHeight(ViewIndex.sidePreferHeight);
        return vb;
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }

    private static final String LOWER_CASE = "abcdefghijklmnopqrstuvwxyz";
    private static final String UPPER_CASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String NUMBERS = "0123456789";
    private static final String SYMBOLS = "!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";

    public static String randomString(boolean useUpperCase, boolean useNumbers, boolean useLowerCase, boolean useSymbols, int length) {
        StringBuilder allowedChars = new StringBuilder();

        if (useLowerCase) allowedChars.append(LOWER_CASE);
        if (useUpperCase) allowedChars.append(UPPER_CASE);
        if (useNumbers) allowedChars.append(NUMBERS);
        if (useSymbols) allowedChars.append(SYMBOLS);

        if (allowedChars.length() == 0) {
            return null;
        }

        Random random = new Random();
        return IntStream.range(0, length)
                .mapToObj(i -> String.valueOf(allowedChars.charAt(random.nextInt(allowedChars.length()))))
                .collect(Collectors.joining());
    }

}
